package dl.jf.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;



import dl.jf.common.constants.Constants;
import dl.jf.common.model.MRecord;
import dl.jf.common.model.MSubRecord;
import dl.jf.dao.idao.ICommonDao;
import dl.jf.dao.idao.IRecordDao;
import dl.jf.service.iservice.IRecordService;
import dl.jf.service.util.Toolkit;

@Transactional(readOnly = true)
public class RecordServiceImpl implements IRecordService {
	private IRecordDao recordDao;
	
	private ICommonDao commonDao;

	public void setCommonDao(ICommonDao commonDao) {
		this.commonDao = commonDao;
	}


	public void setRecordDao(IRecordDao recordDao) {
		this.recordDao = recordDao;
	}


	@Override
	public ArrayList<MRecord> getRecordList(MRecord record) {
		if (!Toolkit.isEmpty(record.getRecordId())) {
			record.setRecordId(record.getRecordId() + "%");
		}
		if (!Toolkit.isEmpty(record.getSubject())) {
			record.setSubject("%" + record.getSubject() + "%");
		}
		return recordDao.getRecordList(record);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void saveRecord(MRecord record) throws Exception {
		String recordId = commonDao.getGenerateCode(Constants.RECORD_NUMBER_CODE,
				Constants.RECORD_NUMBER_LENGTH, Constants.RECORD_NUMBER_PREFIX);
		List<MSubRecord> list = record.getSubRecordList();

		for (int i = 0; i < list.size(); i++) {
			MSubRecord item = list.get(i);
			item.setRecordId(recordId);
			recordDao.saveSubRecord(item);
		}
		record.setRecordId(recordId);
		recordDao.saveRecord(record);
	}
}
